<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'constructProjectInfo:add'"
          >新增项目</el-button
        >
        <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'constructProjectInfo:export'"
          >下载数据</el-button
        >
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入项目名称、业主"
          v-model="parameter.keywords"
          @keydown.enter.native="search"
          @clear="search"
          clearable
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
        <el-date-picker
          @change="search"
          @clear="search"
          v-model="parameter.year"
          type="year"
          placeholder="年度"
          value-format="yyyy"
          :clearable="false"
        />
        <el-select
          placeholder="批次"
          v-model="parameter.batchNo"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in batchSelectionTypeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>

        <el-select
          placeholder="项目类型"
          v-model="parameter.projectBusinessType"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in projectTypeListOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="项目阶段"
          v-model="parameter.maturity"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in maturityOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="是否纳入重要目标统计"
          v-model="parameter.totalFlag"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in yesOrNoOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>

        <el-select
          placeholder="项目状态"
          v-model="parameter.status"
          clearable
          @change="search"
          @clear="search"
        >
          <el-option
            v-for="item in constructProjectStatusOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="年度"
        prop="year"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目名称/立项名称"
        prop="name"
        show-overflow-tooltip
        width="300"
      />
      <el-table-column
        label="项目详细地址"
        prop="address"
        show-overflow-tooltip
      />
      <el-table-column
        label="业主（建设单位）"
        prop="ownerUnit"
        show-overflow-tooltip
      />
      <el-table-column
        label="批次"
        prop="batchNo"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目类型"
        prop="projectBusinessType"
        show-overflow-tooltip
      />
      <el-table-column
        label="总投资（万元）"
        prop="investmentTotal"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="本年度计划投资（万元）"
        prop="planTotal"
        show-overflow-tooltip
      />
      <el-table-column
        label="本年度累计完成投资（万元）"
        prop="planTotalInvestment"
        show-overflow-tooltip
      />
      <el-table-column
        label="本年度计划完成固投（万元）"
        prop="fixedTotal"
        show-overflow-tooltip
      />
      <el-table-column
        label="本年度累计完成固投（万元）"
        prop="fixedTotalInvestment"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目进度状态"
        prop="projectStatus"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="是否纳入重要目标统计"
        prop="totalFlag"
        show-overflow-tooltip
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'constructProjectInfo:edit'"
            >编辑</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'constructProjectInfo:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryConstructionProject,
  deleteConstructionInfo,
  downloadConstructionBaseInfo,
  queryBatchNoOptions,
  queryProjectTypeOptions,
  queryProjectStageOptions
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import {
  batchSelectionOptions,
  projectTypeListOptions,
  maturityOptions,
  projectStatusOptions,
  constructProjectStatusOptions,
  yesOrNoOptions
} from '@/utils/project/data'
import { downloadBlobFile } from '@/utils/download'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'constructProjectInfoList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        projectName: '',
        projectType: '',
        pageIndex: 1,
        pageSize: 10
      },
      buildStageOptions: [],
      loading: false,
      showDialog: false,
      batchSelectionTypeOptions: [],
      projectTypeListOptions: [],
      projectBuildStageTypeOptions: []
    }
  },
  computed: {
    batchSelectionOptions() {
      return batchSelectionOptions
    },
    projectListOptions() {
      return projectTypeListOptions
    },
    maturityOptions() {
      return maturityOptions
    },
    projectStatusOptions() {
      return projectStatusOptions
    },
    constructProjectStatusOptions() {
      return constructProjectStatusOptions
    },
    yesOrNoOptions() {
      return yesOrNoOptions
    }
  },
  mounted() {
    this.getBatchNoOptions()
    this.getProjectTypeOptions()
    this.getProjectStageOptions()
  },
  activated() {
    this.getList()
  },

  methods: {
    // 建设项目批次选项
    getBatchNoOptions() {
      queryBatchNoOptions().then((res) => {
        this.batchSelectionTypeOptions = res.data.length
          ? res.data
          : this.batchSelectionOptions
      })
    },
    // 建设项目项目类型选项
    getProjectTypeOptions() {
      queryProjectTypeOptions().then((res) => {
        this.projectTypeListOptions = res.data.length
          ? res.data
          : this.projectListOptions
      })
    },
    // 建设项目项目阶段选项
    getProjectStageOptions() {
      queryProjectStageOptions().then((res) => {
        this.projectBuildStageTypeOptions = res.data.length
          ? res.data
          : this.projectBuildStageOptions
      })
    },
    search() {
      this.parameter.keywords =
        this.parameter.keywords?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    getList() {
      this.loading = true
      queryConstructionProject(this.parameter).then(
        (res) => {
          if (res.success) {
            this.total = res.data.count
            this.projectList = res.data.list
          }
          this.loading = false
        }
      )
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      this.$router.push({
        path: '/dataManagement/projectManagement/constructProjectInfo/add',
        query: {
          title: '新增',
          isEdit: false,
          isDetail: false
        }
      })
    },

    // 编辑
    editProject(row) {
      this.$router.push({
        path: '/dataManagement/projectManagement/constructProjectInfo/add',
        query: {
          pkid: row.pkid,
          title: '编辑',
          isEdit: true,
          isDetail: false
        }
      })
    },

    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/dataManagement/projectManagement/constructProjectInfo/add',
        query: {
          pkid: row.pkid,
          title: '查看',
          isEdit: false,
          isDetail: true
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该项目，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteConstructionInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      this.btnLoading = true
      downloadConstructionBaseInfo(this.parameter)
        .then((res) => {
          downloadBlobFile(res, '在建项目信息表.xlsx')
        })
        .catch((err) => {
          this.$message.error('下载失败')
        })
        .finally(() => {
          this.btnLoading = false
        })
    },
    handleSubmit(data) {
      console.log('提交进度数据:', data)
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
